package org.xymtop.tbdemo.xmqtt.client;

import cn.hutool.extra.spring.SpringUtil;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.xymtop.tbdemo.xmqtt.ann.XSubscription;
import org.xymtop.tbdemo.xmqtt.hander.ConnectionHander;
import org.xymtop.tbdemo.xmqtt.hander.XHander;

import java.util.*;


@Component
public class XMqttCallback implements MqttCallback {

    @Autowired
    private ConnectionHander connectionHander;

    @Override
    public void connectionLost(Throwable throwable) {
        connectionHander.hander();
    }



    @Override
    public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
        List<XHander> handerList = XSubscriptions.getHanderList(s);
        if (handerList!=null){
            for (XHander hander : handerList){
                mqttMessage =  hander.hander(mqttMessage);
            }
        }

    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {

    }
}
